﻿#include "Tree.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool Tree::getCanMove() const
    {
        return false;
    }

    bool Tree::getCanAttack() const
    {
        return false;
    }

    bool Tree::getCanRegen() const
    {
        return false;
    }

    Tree::Tree(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);
    }

    void Tree::Attack()
    {
    }

    void Tree::FindTarget()
    {
    }

    void Tree::Turn(MirDirection dir)
    {
    }

    bool Tree::Walk(MirDirection dir)
    {
        return false;
    }

    void Tree::ProcessTarget()
    {
    }

    void Tree::ProcessRegen()
    {
    }

    void Tree::ProcessSearch()
    {
    }

    void Tree::ProcessRoam()
    {
    }

    int Tree::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        ShockTime = 0;

        if (attacker->Info->AI == 6)
        {
            EXPOwner = nullptr;
        }
        else if (attacker->Master != nullptr)
        {
            if (EXPOwner == nullptr || EXPOwner->Dead)
            {
                EXPOwner = attacker->Master;
            }

            if (EXPOwner == attacker->Master)
            {
                EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
            }

        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;

    }

    int Tree::Struck(int damage, DefenceType type)
    {
        return 0;
    }

    int Tree::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        if (damageWeapon)
        {
            attacker->DamageWeapon();
        }

        ShockTime = 0;

        if (Master != nullptr && Master != attacker)
        {
            if (getEnvir()->getTime() > Master->BrownTime && Master->getPKPoints() < 200)
            {
                attacker->BrownTime = getEnvir()->getTime() + Settings::Minute;
            }
        }

        if (EXPOwner == nullptr || EXPOwner->Dead)
        {
            EXPOwner = attacker;
        }

        if (EXPOwner == attacker)
        {
            EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        attacker->GatherElement();
        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    void Tree::ApplyPoison(Poison *p, MapObject *Caster, bool NoResist, bool ignoreDefence)
    {
    }
}
